package Client;

import java.util.Date;

import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import Server.VWorldServer;
import VWorld.ClientCallBacks;
import VWorld.ClientCallBacksHelper;
import Client.Controler.AppliManager;
import Client.impl.ClientCallBacksImpl;

public class Utils {

	private static ClientCallBacks clientCB;

	public static ClientCallBacks getClientCallbacks() {
		if (clientCB == null) {
			
				POA rootpoa;
				try {
					if(AppliManager.orb == null){
						System.out.println("orb null : " + new Date().getTime());
					}
					rootpoa = POAHelper.narrow(AppliManager.orb.resolve_initial_references("RootPOA"));
					rootpoa.the_POAManager().activate();

					ClientCallBacksImpl messageBoxImpl = new ClientCallBacksImpl();
					messageBoxImpl.setORB(VWorldServer.orb);

					org.omg.CORBA.Object ref = rootpoa
							.servant_to_reference(messageBoxImpl);
					clientCB = ClientCallBacksHelper.narrow(ref);
				} catch (InvalidName e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (AdapterInactive e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ServantNotActive e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (WrongPolicy e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
		}
		return clientCB;
	}
	
}
